package com.beiye.drivertransport.SubActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.beiye.drivertransport.R;
import com.beiye.drivertransport.activity.TwoBaseAty;
import com.beiye.drivertransport.bean.NewHolehotelBean;
import com.beiye.drivertransport.utils.ChartUtilEarlywarning;
import com.githang.statusbar.StatusBarCompat;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.github.mikephil.charting.formatter.IValueFormatter;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
import com.github.mikephil.charting.utils.ViewPortHandler;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


//预警百分比
public class EarlywarningActivity extends TwoBaseAty {

      LineChart line_frist;//折线图

      TextView tv_des;//

      BarChart bar_frist1;//柱状图

      ImageView img_hiddenback;
      private XAxis xAxis;
      private YAxis leftAxis;
      private YAxis rightAxis;
      ArrayList<String> xVals = new ArrayList<String>();
      @Override
      public int getLayoutId() {
          return R.layout.activity_earlywarning;
      }

      @Override
      public void initData() {
          line_frist = (LineChart)findViewById(R.id.line_frist);
          bar_frist1 = (BarChart)findViewById(R.id.bar_frist1);
          img_hiddenback = (ImageView) findViewById(R.id.img_hiddenback);
          StatusBarCompat.setStatusBarColor(this, Color.parseColor("#1F80C4"));
          initLineChart();
          img_hiddenback.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  finish();
              }
          });



          ChartUtilEarlywarning.initBarchart(bar_frist1);
      }

      @Override
      public void requestData() {
          Bundle bundle = getIntent().getExtras();
          List<NewHolehotelBean.DataBean.InfoLwStatNoBean> listCourse = (List<NewHolehotelBean.DataBean.InfoLwStatNoBean>) bundle.getSerializable("firmlist");
          String name = bundle.getString("name");
          if(listCourse.size()==0)
          {
              return;
          }

          if(name.equals("企业管理隐患统计"))
          {
              tv_des.setText("消除企业隐患预警数");
              ArrayList<Entry> entries = new ArrayList<Entry>();
              for (int i = 0; i < listCourse.size(); i++) {
                  BarEntry barEntry = new BarEntry(i, (float) listCourse.get(i).getFirmStat1per());
                  xVals.add( listCourse.get(i).getStatYmd());
                  entries.add(barEntry);
              }
              // 每一个LineDataSet代表一条线
              LineDataSet lineDataSet = new LineDataSet(entries,"所辖区域企业隐患预警百分比");
              lineDataSet.setValueFormatter(new IValueFormatter() {
                  @Override
                  public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {

                      int n = (int) value;
                      if(n==0)
                      {
                          return "0%";
                      }else{

                          return n+"%";
                      }
                  }
              });
              initLineDataSet(lineDataSet, Color.parseColor("#6bbbec"), true);
              ArrayList<ILineDataSet> dataSets = new ArrayList<>();
              dataSets.add(lineDataSet);
              LineData data = new LineData(dataSets);

              //设置X轴的刻度数
              xAxis.setLabelCount(listCourse.size(), true);
           //   line_frist.moveViewToX(xVals.size() - 1);
              line_frist.setData(data);
              line_frist.invalidate();
              Collections.reverse(listCourse);
              ChartUtilEarlywarning.initCheckData(bar_frist1,listCourse,name);
          }else if(name.equals("从业人员管理隐患统计"))
          {
              tv_des.setText("消除从业人员隐患预警数");
              ArrayList<Entry> entries = new ArrayList<Entry>();
              for (int i = 0; i < listCourse.size(); i++) {
                  BarEntry barEntry = new BarEntry(i, (float) listCourse.get(i).getDriverStat1per());
                  xVals.add( listCourse.get(i).getStatYmd());
                  entries.add(barEntry);
              }
              // 每一个LineDataSet代表一条线
              LineDataSet lineDataSet = new LineDataSet(entries,"所辖区域从业人员隐患预警百分比");
              lineDataSet.setValueFormatter(new IValueFormatter() {
                  @Override
                  public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {

                      int n = (int) value;
                      if(n==0)
                      {
                          return "0%";
                      }else{

                          return n+"%";
                      }
                  }
              });
              initLineDataSet(lineDataSet, Color.parseColor("#6bbbec"), true);
              ArrayList<ILineDataSet> dataSets = new ArrayList<>();
              dataSets.add(lineDataSet);
              LineData data = new LineData(dataSets);

              //设置X轴的刻度数
              xAxis.setLabelCount(listCourse.size(), true);
           //   line_frist.moveViewToX(xVals.size() - 1);
              line_frist.setData(data);
              line_frist.invalidate();
              Collections.reverse(listCourse);
              ChartUtilEarlywarning.initCheckData(bar_frist1,listCourse,name);
          }
              else if(name.equals("车辆装备管理隐患统计"))
          {
              tv_des.setText("消除车辆隐患预警数");
              ArrayList<Entry> entries = new ArrayList<Entry>();
              for (int i = 0; i < listCourse.size(); i++) {
                  BarEntry barEntry = new BarEntry(i, (float) listCourse.get(i).getVehStat1per());
                  xVals.add( listCourse.get(i).getStatYmd());
                  entries.add(barEntry);
              }
              // 每一个LineDataSet代表一条线
              LineDataSet lineDataSet = new LineDataSet(entries,"所辖区域车辆隐患预警百分比");
              lineDataSet.setValueFormatter(new IValueFormatter() {
                  @Override
                  public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {

                      int n = (int) value;
                      if(n==0)
                      {
                          return "0%";
                      }else{

                          return n+"%";
                      }
                  }
              });
              initLineDataSet(lineDataSet, Color.parseColor("#6bbbec"), true);
              ArrayList<ILineDataSet> dataSets = new ArrayList<>();
              dataSets.add(lineDataSet);
              LineData data = new LineData(dataSets);

              //设置X轴的刻度数
              xAxis.setLabelCount(listCourse.size(), true);
          //    line_frist.moveViewToX(xVals.size() - 1);
              line_frist.setData(data);
              line_frist.invalidate();
              Collections.reverse(listCourse);
              ChartUtilEarlywarning.initCheckData(bar_frist1,listCourse,name);
          }




      }
    /**
     * 初始化曲线 每一个LineDataSet代表一条线
     *
     * @param lineDataSet
     * @param color
     * @param mode        折线图是否填充
     */
    private void initLineDataSet(LineDataSet lineDataSet, int color, boolean mode) {
        lineDataSet.setColor(color);
        lineDataSet.setCircleColor(color);
        lineDataSet.setLineWidth(1f);
        lineDataSet.setCircleRadius(3f);
        //设置曲线值的圆点是实心还是空心
        lineDataSet.setDrawCircleHole(false);
        lineDataSet.setValueTextSize(9f);
        //设置折线图填充
        lineDataSet.setDrawFilled(mode);
        lineDataSet.setFormLineWidth(1f);
        lineDataSet.setFormSize(15.f);

        //填充颜色.透明度
        // lineDataSet.setFillColor(Color.BLUE);
        //  lineDataSet.setFillAlpha(35);
        //线模式为圆滑曲线（默认折线）
        lineDataSet.setMode(LineDataSet.Mode.LINEAR);
    }
    /**
     * 初始化LineChart
     */
    private void initLineChart() {
        line_frist.setDrawGridBackground(false);
        //显示边界
        line_frist.setDrawBorders(false);
        line_frist.getDescription().setEnabled(false);
        Description description = new Description();
        description.setText("");//设置右下角的描述信息
        line_frist.setNoDataText("暂无数据"); //设置空表的描述信息
        line_frist.setPinchZoom(true);//挤压缩放
        line_frist.setDoubleTapToZoomEnabled(false);//双击缩放
        line_frist.animateY(2500);
        //设置动画效果
//        mChart.animateY(1000, Easing.EasingOption.Linear);
//        mChart.animateX(1000, Easing.EasingOption.Linear);

        //折线图例 标签 设置
        Legend legend = line_frist.getLegend();
        legend.setForm(Legend.LegendForm.LINE);
        legend.setTextSize(11f);
        //显示位置
        legend.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
        legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
        legend.setOrientation(Legend.LegendOrientation.HORIZONTAL);
        legend.setDrawInside(false);
//        mChart.setPinchZoom(true);//挤压缩放
//        mChart.setScaleYEnabled(false);
//        mChart.setDoubleTapToZoomEnabled(false);//双击缩放
        xAxis = line_frist.getXAxis();
        xAxis.setDrawGridLines(false);
        xAxis.setTextSize(7f);
        //设置x轴的数据
        xAxis.setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {

                int index = (int) value;
                if (index < 0 || index >= xVals.size()) {
                    return "";
                } else {
                    return xVals.get(index);
                }
            }
        });

        leftAxis = line_frist.getAxisLeft();
        leftAxis.setValueFormatter(new IAxisValueFormatter() {
                                       @Override
                                       public String getFormattedValue(float value, AxisBase axis) {
                                           return value + "%";
                                       }
                                   });
        rightAxis = line_frist.getAxisRight();
        rightAxis.setDrawLabels(false);//右侧Y轴数据不显示
        rightAxis.setEnabled(false);
        //XY轴的设置
        //X轴设置显示位置在底部
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setAxisMinimum(0f);
        xAxis.setGranularity(1f);
        //保证Y轴从0开始，不然会上移一点
        leftAxis.setAxisMinimum(0f);
        //   rightAxis.setAxisMinimum(0f);
    }
  }
